/* Compilateur Scalpa : Analyse syntaxique LEX */

/* Code C */
%{
#include "global.h"
#include "yacc.compiler.h"
%}

/* Options LEX */

%option noinput
%option nounput
%option yylineno

/* Gestion des commentaires */

%x COMMENT

/* Expressions régulières */

lettre  [a-z|A-Z]
chiffre [0-9]
integer {chiffre}+
string  \"[^\"]*\"
ident   ({lettre}|"_")({lettre}|{chiffre}|"_")*


/* Règles => Actions */

%%

"(*" BEGIN(COMMENT);

<COMMENT>{
"*)" BEGIN(INITIAL);
.
}

"program"|"PROGRAM" {
	return K_PROGRAM;
}

"begin"|"BEGIN" {
	return K_BEGIN;
}

"end"|"END" {
	return K_END;
}

"var"|"VAR" {
	return K_VAR;
}

"unit"|"UNIT" {
	return K_UNIT;
}

"bool"|"BOOL" {
	return K_BOOL;
}

"int"|"INT" {
	return K_INT;
}

"array"|"ARRAY" {
	return K_ARRAY;
}

"of"|"OF" {
	return K_OF;
}

"function"|"FUNCTION" {
	return K_FUNCTION;
}

"ref"|"REF"  {
	return K_REF;
}

"if"|"IF"  {
	return K_IF;
}

"then"|"THEN" {
	return K_THEN;
}

"else"|"ELSE" {
	return K_ELSE;
}

"while"|"WHILE"  {
	return K_WHILE;
}

"do"|"DO" {
	return K_DO;
}

"return"|"RETURN" {
	return K_RETURN;
}

"read"|"READ" {
	return K_READ;
}

"write"|"WRITE" {
	return K_WRITE;
}

"and"|"AND"  {
	return K_AND;
}

"xor"|"XOR" {
	return K_XOR;
}

"or"|"OR"  {
	return K_OR;
}

"not"|"NOT"  {
	return K_NOT;
}

".." {
	return K_DP;
}

":=" {
	return K_AFF;
}

"[" {
	return '[';
}

"]" {
	return ']';
}

"("  {
	return '(';
}

")"  {
	return ')';
}

":" {
	return ':';
}

";"  {
	return ';';
}

"," {
	return ',';
}

"<=" {
	return K_LE;
}

">=" {
	return K_GE;
}

"<>" {
	return K_NE;
}

"+" {
	return '+';
}

"-" {
	return '-';
}

"*" {
	return '*';
}

"/" {
	return '/';
}

"%" {
	return '%';
}

"<" {
	return '<';
}

">" {
	return '>';
}

"^" {
	return '^';
}

"=" {
	return '=';
}

{integer} {
	yylval.integer = atoi(yytext);
	return INTEGER;
}

"true"|"TRUE" {
	yylval.boolean = TRUE;
	return BOOL;
}

"false"|"FALSE" {
	yylval.boolean = FALSE;
	return BOOL;
}

{string} {
	yylval.string = malloc((strlen(yytext)+1) * sizeof(char));
	yylval.string = strcpy(yylval.string, yytext);
	return STRING;
}

{ident} {
	yylval.string = malloc((strlen(yytext)+1) * sizeof(char));
	yylval.string = strcpy(yylval.string, yytext);
	return IDENT;
}

[\t\n ] {
	// Ignorer
}

. {
	// Si on rencontre un token non reconnu dans les
	// règles précédents, on affiche une erreur :
	yyerror("syntax error");
}


